package com.payment.web.controller.app;

/**
 * @author fuzhili
 * @Description: 积分商城处理
 * @date 2019/8/21 16:24
 */

import com.payment.common.base.AjaxResult;
import com.payment.common.utils.StringUtils;
import com.payment.common.utils.aliyun.OSSClientUtil;
import com.payment.system.domain.ShoppingProduct;
import com.payment.system.service.IShoppingProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/app/shopping")
public class ShoppingController {
    @Autowired
    private IShoppingProductService shoppingProductService;


    /**
     * 获取商品列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/productList")
    @ResponseBody
    public AjaxResult productList(Integer pageNum, Integer pageSize) {
        return shoppingProductService.selectShoppingProductListByApp(pageNum, pageSize);
    }


    /**
     * 上架用的商城获取商品列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/productListSH")
    @ResponseBody
    public AjaxResult productListSH(Integer pageNum, Integer pageSize) {
        return shoppingProductService.selectShoppingProductListByAppSH(pageNum, pageSize);
    }



    /**
     * 商品详情
     * @param productId
     * @return
     */
    @RequestMapping("/productDetails")
    @ResponseBody
    public AjaxResult productDetails(String productId) {
        return shoppingProductService.selectProductDetails(productId);
    }

    /**
     * 商品详情
     * @param productId
     * @return
     */
    @RequestMapping("/productDetailsSH")
    @ResponseBody
    public AjaxResult productDetailsSH(String productId) {
        if (StringUtils.isEmpty(productId)) {
            return AjaxResult.error(1, "获取商品详情失败，请上传商品id");
        }
        ShoppingProduct shoppingProduct = shoppingProductService.selectShoppingProductById(productId);
        if (shoppingProduct == null) {
            return AjaxResult.error(1, "暂无该商品信息！");
        }
        if (shoppingProduct.getImg() != null) {
            if (shoppingProduct.getImg().contains(";")) {
                String urls = "";
                String[] url = shoppingProduct.getImg().split(";");
                StringBuffer stb = new StringBuffer();
                for (int i = 0; i < url.length; i++) {
                    urls = stb.append(OSSClientUtil.getImgUrl(url[i]) + ";").toString();
                }

                shoppingProduct.setImg(urls.substring(0,urls.length()-1));
            } else {
                shoppingProduct.setImg(OSSClientUtil.getImgUrl(shoppingProduct.getImg()));
            }

        }
        return AjaxResult.successAndData("获取商品详情成功！", shoppingProduct);
    }

}
